package com.zhai.boot.util;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

/**
 * @author Created by 翟凤玺
 * @package com.zhai.boot.util
 * @project-name zhai-boot
 * @description 反射常用工具类
 * @create-datetime 2019-1-24 18:27
 */
public class ClassUtil {

    /**
     * 比较两个类
     */
    public static boolean compareClass(Object obj1, Object obj2) {
        if (obj1.getClass() == obj2.getClass()) {
            return true;
        }
        return false;
    }

    /**
     * 获取类信息
     */
    public static PropertyDescriptor[] getClassProperty(Class clazz) {
        try {
            return Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors();
        } catch (IntrospectionException e) {
            e.printStackTrace();
            throw new RuntimeException("获取类信息失败");
        }
    }

    /**
     * 获取属性名
     */
    public static String getPropertyName(PropertyDescriptor pd) {
        return pd.getName();
    }

    /**
     * 获取属性值
     */
    public static Object getPropertyValue(PropertyDescriptor pd, Object obj) {
        try {
            return pd.getReadMethod().invoke(obj);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取属性值失败");
        }
    }

    /**
     * 获取属性类型
     */
    public static Object getPropertyType(PropertyDescriptor pd) {
        return pd.getPropertyType();
    }

}
